﻿using Biomedica.Nui.ui;
using NuiDicomPlayer.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace NuiDicomPlayer.Subviews
{
    /// <summary>
    /// This is the base class that all the Views will be drived from
    /// </summary>
    public class SubviewBase : UserControl
    {
        #region Events
        public static readonly RoutedEvent ViewAppearRoutedEvent
            = EventManager.RegisterRoutedEvent("OnViewAppear", RoutingStrategy.Bubble, typeof(EventHandler), typeof(SubviewBase));

        public static readonly RoutedEvent ViewDisappearRoutedEvent
            = EventManager.RegisterRoutedEvent("OnViewDisappear", RoutingStrategy.Bubble, typeof(EventHandler), typeof(SubviewBase));
        public event EventHandler OnViewAppear
        {
            add { AddHandler(ViewAppearRoutedEvent, value); }
            remove { RemoveHandler(ViewAppearRoutedEvent, value); }
        }

        public event EventHandler OnViewDisappear
        {
            add { AddHandler(ViewDisappearRoutedEvent, value); }
            remove { RemoveHandler(ViewDisappearRoutedEvent, value); }
        }
        #endregion
        public DialogResults WindowResult { set; get; }
        
        //private void HandTestHits()
        //{
        //    foreach (NuiControl cnt in UserControlTransition.FindVisualChildren<NuiControl>(this))
        //    {
        //        cnt.HandHitTest(App.ActiveMouse);
        //        // Check for Gravity
        //        cnt.CheckIfMagnitised(App.ActiveMouse);
        //    }
        //}
        public virtual void ShowView()
        {            
            this.Visibility = Visibility.Visible;
            App.ActiveMouse.Enabled = false; ;
            Biomedica.Nui.ui.UserControlTransition.DoControlTransitionAnimation(this,-this.ActualHeight,0, 1, 1000, OnlocalStartTransitionEnded);
            
        }
        public void OnlocalStartTransitionEnded(object sender, EventArgs e)
        {
            App.ActiveWindow = this;
            App.ActiveMouse.Enabled = true;
            RaiseEvent(new RoutedEventArgs(ViewAppearRoutedEvent, this));
        }
        public virtual void HideView()
        {
            App.ActiveMouse.Enabled = false;
            Biomedica.Nui.ui.UserControlTransition.DoControlTransitionAnimation(this,0, -this.ActualHeight-32, 1, 1000, OnLocalEndTransitionEnded);
        }
        public virtual void OnLocalEndTransitionEnded(object sender, EventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
            App.ActiveMouse.Enabled = true;
            RaiseEvent(new RoutedEventArgs(ViewDisappearRoutedEvent, this));
        }
        
        public void TriggerClose()
        {
            this.HideView();
        }

        internal virtual void DoSpeechCommand(System.Speech.Recognition.RecognitionResult recognitionResult)
        {
        }
       
    }
    [Flags()]
    public enum DialogResults
    {
        OK,
        CANCLE,
        ABORT
    }
 }